package com.tinusgraglin.enclusiv.ui.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tinusgraglin.enclusiv.EnclusivApp
import com.tinusgraglin.enclusiv.R
import com.tinusgraglin.enclusiv.Util
import com.tinusgraglin.enclusiv.datalayer.repos.GlobalPrefDataRepo
import com.tinusgraglin.enclusiv.datalayer.repos.UserRepo

class SignInActivityViewModel(
    private val prefDataRepo: GlobalPrefDataRepo,
    private val userDataRepo: UserRepo
    ) : ViewModel()
{
    private val prefStringRemember = EnclusivApp.appContext.getString(R.string.main_pref_remember_me)
    private val prefStringUserEmail = EnclusivApp.appContext.getString(R.string.main_pref_username_remembered)
    private val prefStringUserPassword = EnclusivApp.appContext.getString(R.string.main_pref_password_remembered)

    var refRememberUser: Boolean
        get() = prefDataRepo.getBoolean(prefStringRemember, false)
        set(value) {
            prefDataRepo.edit {
                putBoolean(prefStringRemember, value)
            }
        }

    var refUserEmailStored: String?
        get() = prefDataRepo.getString(prefStringUserEmail, null)
        set(value) {
            prefDataRepo.edit {
                putString(prefStringUserEmail, value)
            }
        }

    var refUserPasswordStored: String?
        get() = prefDataRepo.getString(prefStringUserPassword, null)
        set(value) {
            prefDataRepo.edit {
                putString(prefStringUserPassword, value)
            }
        }

    fun loginAsync(email: String, password: String) =
        Util.requestWithDeferredResultAsync(viewModelScope) {
            userDataRepo.getUser(email, password)
        }
}